package com.quanyan.place.web;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbBizPlaceFinanceAudit;
import com.quanyan.place.entity.apireq.ReqFinanceMeasureInfo;
import com.quanyan.place.entity.apireq.ReqOrderOperate;
import com.quanyan.place.entity.apireq.ReqPlaceRevenueStreamVo;
import com.quanyan.place.entity.vo.PlaceFinanceAuditVo;
import com.quanyan.place.entity.vo.ReqFinanceAudit;
import com.quanyan.place.entity.vo.ReqFinanceAuditSearch;
import com.quanyan.place.web.api.CallbackController;
import com.quanyan.place.wrapper.FinanceMeasureWrapper;
import com.quanyan.place.wrapper.FinanceWrapper;
import com.quanyan.place.wrapper.PlaceOrderServiceWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * 财务
 * Created by liuyaoxin on 16/7/26.
 */
@RestController
@RequestMapping("/admin/api/place/finance")
public class FinanceController {

    @Autowired
    FinanceMeasureWrapper financeMeasureWrapper;

    @Autowired
    FinanceWrapper financeWrapper;

    @Autowired
    PlaceOrderServiceWrapper placeOrderServiceWrapper;

    private final static Logger logger = LoggerFactory.getLogger(FinanceController.class);

    /**
     * 盈亏列表
     * @param reqFinanceMeasureInfo
     * @return
     */
    @RequestMapping(value = "/queryFinanceDataByCategoryView", method=RequestMethod.POST)
    public APIResponse<?>queryFinanceDataByCategoryView(@RequestBody ReqFinanceMeasureInfo reqFinanceMeasureInfo){
        return  financeMeasureWrapper.queryFinanceDataByCategoryView(reqFinanceMeasureInfo);

    }

    /**
     * 盈亏列表导出
     * @param reqFinanceMeasureInfo
     * @return
     */
    @RequestMapping(value = "/queryFinanceDataByCategoryViewReport", method=RequestMethod.POST)
    public APIResponse<?>queryFinanceDataByCategoryViewReport(@RequestBody ReqFinanceMeasureInfo reqFinanceMeasureInfo){
        return  APIResponse.returnSuccess(financeMeasureWrapper.queryFinanceDataByCategoryViewReport(reqFinanceMeasureInfo));

    }

    /**
     * 分馆列表
     * @param reqFinanceMeasureInfo
     * @return
     */
    @RequestMapping(value = "/queryFinanceDataByPlaceView", method=RequestMethod.POST)
    public APIResponse<?>queryFinanceDataByPlaceView(@RequestBody ReqFinanceMeasureInfo reqFinanceMeasureInfo){
        return  financeMeasureWrapper.queryFinanceDataByPlaceView(reqFinanceMeasureInfo);
    }

    /**
     * 分馆列表导出
     * @param reqFinanceMeasureInfo
     * @return
     */
    @RequestMapping(value = "/queryFinanceDataByPlaceViewReport", method=RequestMethod.POST)
    public APIResponse<?>queryFinanceDataByPlaceViewReport(@RequestBody ReqFinanceMeasureInfo reqFinanceMeasureInfo){
        return  APIResponse.returnSuccess(financeMeasureWrapper.queryFinanceDataByPlaceViewReport(reqFinanceMeasureInfo));
    }


    @RequestMapping(value = "/importFinanceDataByCategory", method=RequestMethod.GET)
    public APIResponse<?>importFinanceDataByCategory( ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        financeMeasureWrapper.importFinanceDataByCategory(reqFinanceMeasureInfo);
        return APIResponse.returnSuccess();
    }
    /**
     * 场馆收入流水
     * @param reqPlaceRevenueStreamVo
     * @return
     */
    @RequestMapping(value = "/getPlaceRevenueStream", method = RequestMethod.POST,produces = {"application/json; charset=UTF-8"})
    public APIResponse<?> getPlaceRevenueStream(@RequestBody @Valid ReqPlaceRevenueStreamVo reqPlaceRevenueStreamVo){
        return  APIResponse.returnSuccess(financeMeasureWrapper.getPlaceRevenueStream(reqPlaceRevenueStreamVo));
    }

    /**
     * 场馆收入流水导出
     * @param reqPlaceRevenueStreamVo
     * @return
     */
    @RequestMapping(value = "/getPlaceRevenueStreamExport", method = RequestMethod.POST,produces = {"application/json; charset=UTF-8"})
    public APIResponse<?> getPlaceRevenueStreamExport(@RequestBody @Valid ReqPlaceRevenueStreamVo reqPlaceRevenueStreamVo){
        return  APIResponse.returnSuccess(financeMeasureWrapper.getPlaceRevenueStreamExport(reqPlaceRevenueStreamVo));
    }

    /**
     * 获取审核列表
     * @return
     */
    @RequestMapping(value = "/getPlaceFinanceAuditList", method=RequestMethod.POST)
    public APIResponse<?> getPlaceFinanceAuditList(@RequestBody @Valid ReqFinanceAuditSearch reqSearch){
        PageObj<List<PlaceFinanceAuditVo>> result = financeWrapper.getPlaceFinanceAuditList(reqSearch);
        return APIResponse.returnSuccess(result);
    }

    /**
     * 财务初审
     * @param reqFinanceAudit
     * @return
     */
    @RequestMapping(value = "/executeFirstAudit", method = RequestMethod.POST)
    public APIResponse<?> executeFirstAudit(@RequestBody ReqFinanceAudit reqFinanceAudit)  {
        // 审核记录
        APIResponse ret = financeWrapper.validAuditInfo(reqFinanceAudit);
        if (!ret.isRet()){
            return ret;
        }
        TbBizPlaceFinanceAudit tbBizPlaceFinanceAudit = financeWrapper.getPlaceFinanceAudit(reqFinanceAudit.getId());
        if (null == tbBizPlaceFinanceAudit){
            return APIResponse.returnFail("不存在审核记录");
        }
        if ((PlaceFinanceAuditVo.FINANCE_AUDIT_PUBLIC_TRANSFER_ORDER.equals(tbBizPlaceFinanceAudit.getBusinessType()) ||
                PlaceFinanceAuditVo.FINANCE_AUDIT_PUBLIC_TRANSFER_CHARGE.equals(tbBizPlaceFinanceAudit.getBusinessType())) &&
                PlaceFinanceAuditVo.AUDTI_RESULT_OK.equals(reqFinanceAudit.getResult())){
            // 对公转账开单和对公转账开卡充值 修改相应订单状态
            ReqOrderOperate reqOrderOperatePaySuccess = new ReqOrderOperate();
            reqOrderOperatePaySuccess.setUid(reqFinanceAudit.getUid());
            reqOrderOperatePaySuccess.setOrderNo(tbBizPlaceFinanceAudit.getBusinessNo());
            // 审核通过
            tbBizPlaceFinanceAudit.setFirstAuditStatus(PlaceFinanceAuditVo.STATUS_AUDIT_PASS);
            reqOrderOperatePaySuccess.setOperateType(PlaceConstants.ORDER_PAY_CONFIRM_OPERATE); // 订单完成
            reqOrderOperatePaySuccess.setUserType(PlaceConstants.ORDER_OPERATE_ADMINUSER);
            reqOrderOperatePaySuccess.setOrderOperateKey(PlaceConstants.ORDER_OPERATE_KEY);
            reqOrderOperatePaySuccess.setPayTime(new Date());
            APIResponse apiResponse =  placeOrderServiceWrapper.orderOperate(reqOrderOperatePaySuccess);
            if(apiResponse.isRet()){
                apiResponse = financeWrapper.executeFirstAudit(reqFinanceAudit, tbBizPlaceFinanceAudit);
                logger.warn("对公转账订单状态更新成功，操作员：{}，订单号：{} ",reqFinanceAudit.getUid(), tbBizPlaceFinanceAudit.getBusinessNo());
            }
            return apiResponse;
        } else {
            APIResponse apiResponse = financeWrapper.executeFirstAudit(reqFinanceAudit, tbBizPlaceFinanceAudit);
            return apiResponse;
        }

    }


}
